/*
 * @Author: 胡海
 * @Date: 2020-02-10 19:39:49
 * @LastEditors: 胡海
 * @LastEditTime: 2020-03-11 19:47:42
 * @Description: 
 */
const Router = require('koa-router')
const fs = require('fs')
const path = require('path')
const {
    RegisterValidator,
    deRegisterValidator,
    addInfoValidator,
    searchUserListValidator,
    rolesMenuValidator
} = require('../../validators/validator')
const {
    upRegisterValidator,
    editPasswordValidator,
    addRolesValidator,
    upRolesValidator,
    RolesBtnListValidator
} = require('../../validators/user')

const {
    Auth
} = require('../../../middlewares/auth')
const {
    User
} = require('../../models/user.js')

const {
    UserInfo
} = require('../../models/userInfo.js')

const {
    UserRoles
} = require('../../models/userRoles.js')

const router = new Router({
    prefix: '/v1/user'
})

router.post('/register', async (ctx) => { // 注册
    const v = await new RegisterValidator().validate(ctx)
    let user = {
        username: v.get('body.username'),
        password: v.get('body.password1'),
        type: v.get('body.type'),
    }
    const p = await User.create(user)
    throw new global.errs.Success()
})

router.post('/editPassword', new Auth().m, async (ctx) => { // 修改用户密码
    const v = await new editPasswordValidator().validate(ctx)
    const p = await User.editPassword(ctx, v)
})
router.post('/upRegister', new Auth().m, async (ctx) => { // 修改用户
    const v = await new upRegisterValidator().validate(ctx)
    const p = await User.upRegister(ctx, v)
})
router.post('/deRegister', new Auth().m, async (ctx) => { // 删除用户
    const v = await new deRegisterValidator().validate(ctx)
    const p = await User.deRegister(ctx, v)
})
router.post('/userList', new Auth().m, async (ctx) => { // 用户列表
    const v = await new searchUserListValidator().validate(ctx)
    const data = await User.getUserList(ctx, v)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data
    }
})

router.post('/rolesList', new Auth().m, async (ctx) => { // 角色列表
    const v = await new searchUserListValidator().validate(ctx)
    const data = await UserRoles.getRolesList(ctx, v)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data
    }
})

router.post('/addRoles', new Auth().m, async (ctx) => { // 新增角色
    const v = await new addRolesValidator().validate(ctx)
    const p = await UserRoles.addRoles(ctx, v)
})

router.post('/upRoles', new Auth().m, async (ctx) => { // 修改角色
    const v = await new upRolesValidator().validate(ctx)
    const p = await UserRoles.upRoles(ctx, v)
})

router.post('/rolesMenu', new Auth().m, async (ctx) => { // 角色菜单
    const v = await new rolesMenuValidator().validate(ctx)
    const p = await UserRoles.setRolesMenu(ctx, v)
})
router.post('/rolesBtn', new Auth().m, async (ctx) => { // 角色按钮列表
    const v = await new RolesBtnListValidator().validate(ctx)
    const data = await UserRoles.RolesBtnList(ctx, v)
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data
    }
})

router.get('/info', new Auth().m, async (ctx) => { // 信息
    const user = await User.findOne({
        where: {
            id: ctx.auth.uid
        }
    })
    const info = await UserRoles.findOne({
        where: {
            rolesType: user.type
        }
    })

    const data = {
        routes: JSON.parse(info.menu),
        user
    }
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data
    }
})
router.post('/upload', async (ctx) => { // 上传
    const file = ctx.request.files.file // 获取上传文件
    const reader = fs.createReadStream(file.path);
    // const fileName = `${new Date().getTime()}${/\.[^\.]+$/.exec(file.name)}`
    const fileName = `${new Date().getTime()}.jpg`
    let filePath = path.join(__dirname, '../../../static/img/') + `/${fileName}`;
    // 创建可写流
    const upStream = fs.createWriteStream(filePath);
    // 可读流通过管道写入可写流
    reader.pipe(upStream);
    ctx.body = {
        error_code: 0,
        msg: 'ok',
        data: {
            avatar: `img/${fileName}`
        }
    }
})




module.exports = router